package com.lx.boot1;


import com.lx.boot.OS;
import com.lx.boot.web.AuthUtil;
import com.lx.boot.web.annotation.Auth;
import com.lx.constant.DefaultResult;
import com.lx.entity.Result;
import com.lx.entity.UserInfo;
import com.lx.entity.Var;
import com.lx.util.LX;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name = "无需登录验证加密接口")
@RequestMapping("/NA")
@Slf4j
public class NotAuthController {

    @Autowired
    private AuthUtil authUtil;

    @Operation(summary = "xxx")
    @GetMapping("/login")
    @Auth(value = false, ipWhiteListRegexApplicationKey = "test.ip.list")
    public Result login(String userId, String type){
        if(!OS.allowAccessByCallFrequency(userId,"3/30s")){
            return DefaultResult.CALL_LIMIT.getResult();
        }
//        if (!OS.allowAccessByIp("127.0.*")){
//            return DefaultResult.IP_LIMIT.getResult();
//        }
        return Result.ok(authUtil.createToken(new UserInfo(userId,type)));
    }

    @Operation(summary = "登录")
    @PostMapping("/login1")
    @Auth(value = false)
    public Result login1(@RequestBody @Valid User user){
        if(!OS.allowAccessByCallFrequency(user.username,"3/30s")){
            return DefaultResult.CALL_LIMIT.getResult();
        }
        UserInfo userInfo = new UserInfo(user.username, user.userType);
        Var var = LX.toMap(userInfo).put("token", authUtil.createToken(userInfo));
        return Result.ok(var);
    }

    @Data
    public static class User{
        @NotBlank(message = "用户名不能为空!")
        private String username;
        @NotBlank(message = "密码不能为空!")
        private String password;
        @NotBlank(message = "用户类型不能为空")
        private String userType;
    }

}
